<template>
  <div class="content">
    <div class="header">
      <h3 class="title">用户列表</h3>
      <el-button v-if="isCreate" type="primary" @click="handleNewUserClick"
        >新建用户</el-button
      >
    </div>
    <div class="table">
      <!-- <ul v-if="userList">
        <li v-for="item in userList" :key="item.id">{{ item.name }}</li>
      </ul> -->
      <el-table :data="userList" style="width: 100%">
        <el-table-column align="center" type="selection"></el-table-column>
        <el-table-column
          align="center"
          label="序号"
          type="index"
        ></el-table-column>

        <el-table-column
          align="center"
          label="用户名"
          prop="name"
        ></el-table-column>
        <el-table-column
          align="center"
          label="真实姓名"
          prop="realname"
        ></el-table-column>
        <el-table-column
          width="150px"
          align="center"
          label="手机号码"
          prop="cellphone"
        ></el-table-column>
        <el-table-column align="center" label="状态" prop="enable" width="80px">
          <!-- 作用域插槽-自定义 -->
          <!-- 启用禁用  scoped一行的信息  -->
          <template #default="scoped">
            <!-- 拿数据做判断 -->
            <el-button plain :type="scoped.row.enable ? 'primary' : 'danger'">
              {{ scoped.row.enable ? '启用' : '禁用' }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          width="180px"
          align="center"
          label="创建时间"
          prop="createAt"
        >
          <template #default="scoped">
            <!-- 时间数据格式化 -->
            {{ formatUTC(scoped.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="更新时间"
          prop="updateAt"
          width="180px"
        >
          <template #default="scoped">
            <!-- 时间数据格式化 -->
            {{ formatUTC(scoped.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="200px" label="操作">
          <template #default="scoped">
            <el-button
              icon="edit"
              @click="handleEditeClick(scoped.row)"
              type="primary"
              text
              v-if="isUpdate"
              >编辑</el-button
            >
            <el-button
              v-if="isDelete"
              @click="handleDeleteClick(scoped.row.id)"
              icon="delete"
              type="danger"
              text
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagenation">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[1, 2, 3]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="usersTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import useSystemStore from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { formatUTC } from '@/utils/format'
import usePermissions from '@/hooks/usePermissons'
const emit = defineEmits(['newClick', 'editClick'])
// 按钮权限判断
const isCreate = usePermissions('users:create')
const isUpdate = usePermissions('users:update')
const isDelete = usePermissions('users:delete')
const isQuery = usePermissions('users:query')
const currentPage = ref(1)
const pageSize = ref(10)
const systemStore = useSystemStore()
// const queryInfo = ref({})
//1发送网络请求 异步的  传递查询参数
// systemStore.postUserListAction(queryInfo)
// 一进来就调用
fetchUserListData()
// 第一次式空数据
// 2获取数据 做展示 解构  改成响应式的  要依赖一下计算属性 数据变化会再次展示
const { userList, usersTotalCount } = storeToRefs(systemStore)
// console.log(userList)
// 3，页码相关的逻辑
function handleSizeChange() {
  // console.log('page-size', pageSize.value)
  fetchUserListData()
}
function handleCurrentChange() {
  // console.log('current-page', currentPage.value)
  fetchUserListData()
}
// 4，定义函数用于发送网络请求
function fetchUserListData(formData: any = {}) {
  if (!isQuery) return
  // 查询参数
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size //偏移量 相当于查询第几页的数据
  const pageInfo = { size, offset }
  const queryInfo = { ...pageInfo, ...formData }
  // 发送网络请求
  systemStore.postUserListAction(queryInfo)
}
// 5,删除
function handleDeleteClick(id: number) {
  // console.log('delete-click', id)
  // 删除完
  systemStore.deleteUserByIdAction(id)
  // 重新请求数据我们也可以在store中调用方法
  // fetchUserListData({ offset: 0, size: 10 })
}
// 7,编辑
function handleEditeClick(itemData: any) {
  // console.log('edit-click', itemData)
  emit('editClick', itemData)
}
// 6新建用户
function handleNewUserClick() {
  console.log('新建用户')
  emit('newClick')
}
// 暴露数据
defineExpose({ fetchUserListData })
</script>
<style lang="less" scoped>
.content {
  background-color: #fff;
  padding: 20px;
  margin-top: 20px;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
  .title {
    font-size: 22px;
  }
}
.el-table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin: 0;
    padding: 5px 8px;
  }
}
.pagenation {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
